package com.cyl.manager.yee.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.manager.yee.domain.YeeMerchant;
import com.cyl.manager.yee.domain.YeeMerchantSif;
import com.cyl.manager.yee.mapper.YeeMerchantSifMapper;
import com.cyl.manager.yee.pojo.query.YeeMerchantSifAdd;
import com.cyl.manager.yee.pojo.query.YeeMerchantSifCwsp;
import com.cyl.manager.yee.pojo.query.YeeMerchantSifQuery;
import com.cyl.yeepay.config.YeePayConfig;
import com.cyl.yeepay.setvice.YeePayService;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SortUtil;
import com.ruoyi.system.service.ISysConfigService;
import com.yeepay.yop.sdk.service.account.model.TransferB2bOrderMgTransferOrderRespDTOResult;
import com.yeepay.yop.sdk.service.account.model.WithdrawOrderWithdrawOrderRespDTOResult;

/**
 * 结算记录Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class YeeMerchantSifService {
    @Autowired
    private YeeMerchantSifMapper yeeMerchantSifMapper;

    @Autowired
    private YeeMerchantService yeeMerchantService;
    
    @Autowired
    private YeePayService yeePayService;
    
    @Autowired
    private YeePayConfig yeePayConfig;
    
    @Autowired
    private ISysConfigService configService;
    
    private final static BigDecimal payRate = new BigDecimal("0.003");//费率

    private BigDecimal getFeeAmount(BigDecimal payAmount){
    	if(payAmount==null) {
            throw new RuntimeException("结算金额不能为空");
    	}
    	if (payAmount.compareTo(BigDecimal.ZERO) < 0) {
            throw new RuntimeException("结算金额不能小于0");
        } 
        return payAmount.multiply(YeeMerchantSifService.payRate).setScale(2, RoundingMode.HALF_UP);
    }
    
    
    
    public static void main(String[] args) {
    	BigDecimal payAmount = new BigDecimal("1.68");
    	 
    	if (payAmount.compareTo(BigDecimal.ZERO) < 0) {
            throw new RuntimeException("结算金额不能小于0");
        } 
    	BigDecimal payAmount2 =    payAmount.multiply(YeeMerchantSifService.payRate).setScale(2, RoundingMode.HALF_UP);
         System.out.println(payAmount2);
         
	}
    /**
     * 查询结算记录
     *
     * @param id 结算记录主键
     * @return 结算记录
     */
    public YeeMerchantSif selectById(Long id) {
        return yeeMerchantSifMapper.selectById(id);
    }

    /**
     * 查询结算记录列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 结算记录
     */
    public List<YeeMerchantSif> selectList(YeeMerchantSifQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize(), SortUtil.sort2string(page.getSort(),"id desc"));
        }
        QueryWrapper<YeeMerchantSif> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        
        String merchantBz = query.getMerchantBz();
        if (!StringUtils.isEmpty(merchantBz)) {
            qw.like("merchant_bz", "%".concat(merchantBz.trim()).concat("%"));
        }
        
        String merchantMc = query.getMerchantMc();
        if (!StringUtils.isEmpty(merchantMc)) {
            qw.like("merchant_mc", "%".concat(merchantMc.trim()).concat("%"));
        }
        String merchantFl = query.getMerchantFl();
        if (!StringUtils.isEmpty(merchantFl)) {
            qw.eq("merchant_fl", merchantFl);
        }
        String merchantLx = query.getMerchantLx();
        if (!StringUtils.isEmpty(merchantLx)) {
            qw.eq("merchant_lx", merchantLx);
        }
        String merchantNo = query.getMerchantNo();
        if (!StringUtils.isEmpty(merchantNo)) {
            qw.eq("merchant_no", merchantNo);
        }
        String merchantPwd = query.getMerchantPwd();
        if (!StringUtils.isEmpty(merchantPwd)) {
            qw.eq("merchant_pwd", merchantPwd);
        }
        String bankCardType = query.getBankCardType();
        if (!StringUtils.isEmpty(bankCardType)) {
            qw.eq("bank_card_type", bankCardType);
        }
        String accountNo = query.getAccountNo();
        if (!StringUtils.isEmpty(accountNo)) {
            qw.eq("account_no", accountNo);
        }
        String bankCode = query.getBankCode();
        if (!StringUtils.isEmpty(bankCode)) {
            qw.eq("bank_code", bankCode);
        }
        String branchCode = query.getBranchCode();
        if (!StringUtils.isEmpty(branchCode)) {
            qw.eq("branch_code", branchCode);
        }
        String bindId = query.getBindId();
        if (!StringUtils.isEmpty(bindId)) {
            qw.eq("bind_id", bindId);
        }
        String requestNo = query.getRequestNo();
        if (!StringUtils.isEmpty(requestNo)) {
            qw.eq("request_no", requestNo);
        }
        String fromMerchantNo = query.getFromMerchantNo();
        if (!StringUtils.isEmpty(fromMerchantNo)) {
            qw.eq("from_merchant_no", fromMerchantNo);
        }
        String toMerchantNo = query.getToMerchantNo();
        if (!StringUtils.isEmpty(toMerchantNo)) {
            qw.eq("to_merchant_no", toMerchantNo);
        }
        String toAccountType = query.getToAccountType();
        if (!StringUtils.isEmpty(toAccountType)) {
            qw.eq("to_account_type", toAccountType);
        }
        BigDecimal orderAmount = query.getOrderAmount();
        if (orderAmount != null) {
            qw.eq("order_amount", orderAmount);
        }
        BigDecimal feeAmount = query.getFeeAmount();
        if (feeAmount != null) {
            qw.eq("fee_amount", feeAmount);
        }
        String feeZt = query.getFeeZt();
        if (!StringUtils.isEmpty(feeZt)) {
            qw.eq("fee_zt", feeZt);
        }
        LocalDateTime[] feeTimeRange = query.getFeeTimeRange();
        if (feeTimeRange != null && feeTimeRange.length == 2) {
            LocalDateTime start = feeTimeRange[0];
            if (start != null) {
                qw.ge("fee_time", start);
            }
            LocalDateTime end = feeTimeRange[0];
            if (end != null) {
                qw.le("fee_time", end);
            }
        }
        String feeYy = query.getFeeYy();
        if (!StringUtils.isEmpty(feeYy)) {
            qw.eq("fee_yy", feeYy);
        }
        String b2bZt = query.getB2bZt();
        if (!StringUtils.isEmpty(b2bZt)) {
            qw.eq("b2b_zt", b2bZt);
        }
        String b2cZt = query.getB2cZt();
        if (!StringUtils.isEmpty(b2cZt)) {
            qw.eq("b2c_zt", b2cZt);
        }
        LocalDateTime[] b2bTimeRange = query.getB2bTimeRange();
        if (b2bTimeRange != null && b2bTimeRange.length == 2) {
            LocalDateTime start = b2bTimeRange[0];
            if (start != null) {
                qw.ge("b2b_time", start);
            }
            LocalDateTime end = b2bTimeRange[0];
            if (end != null) {
                qw.le("b2b_time", end);
            }
        }
        LocalDateTime[] b2cTimeRange = query.getB2cTimeRange();
        if (b2cTimeRange != null && b2cTimeRange.length == 2) {
            LocalDateTime start = b2cTimeRange[0];
            if (start != null) {
                qw.ge("b2c_time", start);
            }
            LocalDateTime end = b2cTimeRange[0];
            if (end != null) {
                qw.le("b2c_time", end);
            }
        }
        String b2bYy = query.getB2bYy();
        if (!StringUtils.isEmpty(b2bYy)) {
            qw.eq("b2b_yy", b2bYy);
        }
        String b2cYy = query.getB2cYy();
        if (!StringUtils.isEmpty(b2cYy)) {
            qw.eq("b2c_yy", b2cYy);
        }
        String b2cBz = query.getB2cBz();
        if (!StringUtils.isEmpty(b2cBz)) {
            qw.eq("b2c_bz", b2cBz);
        }
        String b2cRequestNo = query.getB2cRequestNo();
        if (!StringUtils.isEmpty(b2cRequestNo)) {
            qw.eq("b2c_request_no", b2cRequestNo);
        }
        String b2bRequestNo = query.getB2bRequestNo();
        if (!StringUtils.isEmpty(b2bRequestNo)) {
            qw.eq("b2b_request_no", b2bRequestNo);
        }
        String feeRequestNo = query.getFeeRequestNo();
        if (!StringUtils.isEmpty(feeRequestNo)) {
            qw.eq("fee_request_no", feeRequestNo);
        }
        String jsfl = query.getJsfl();
        if (!StringUtils.isEmpty(jsfl)) {
            qw.eq("jsfl", jsfl);
        }
        Long cnryid = query.getCnryid();
        if (cnryid != null) {
            qw.eq("cnryid", cnryid);
        }
        String cnryxm = query.getCnryxm();
        if (!StringUtils.isEmpty(cnryxm)) {
            qw.eq("cnryxm", cnryxm);
        }
        LocalDateTime[] cntjTimeRange = query.getCntjTimeRange();
        if (cntjTimeRange != null && cntjTimeRange.length == 2) {
            LocalDateTime start = cntjTimeRange[0];
            if (start != null) {
                qw.ge("cntj_time", start);
            }
            LocalDateTime end = cntjTimeRange[0];
            if (end != null) {
                qw.le("cntj_time", end);
            }
        }
        String cmtjyy = query.getCmtjyy();
        if (!StringUtils.isEmpty(cmtjyy)) {
            qw.eq("cmtjyy", cmtjyy);
        }
        Long cwryid = query.getCwryid();
        if (cwryid != null) {
            qw.eq("cwryid", cwryid);
        }
        String cwryxm = query.getCwryxm();
        if (!StringUtils.isEmpty(cwryxm)) {
            qw.eq("cwryxm", cwryxm);
        }
        LocalDateTime[] cwtjTimeRange = query.getCwtjTimeRange();
        if (cwtjTimeRange != null && cwtjTimeRange.length == 2) {
            LocalDateTime start = cwtjTimeRange[0];
            if (start != null) {
                qw.ge("cwtj_time", start);
            }
            LocalDateTime end = cwtjTimeRange[0];
            if (end != null) {
                qw.le("cwtj_time", end);
            }
        }
        String jszt = query.getJszt();
        if (!StringUtils.isEmpty(jszt)) {
            qw.eq("jszt", jszt);
        }
        Long deptZbjId = query.getDeptZbjId();
        if (deptZbjId != null) {
            qw.eq("dept_zbj_id", deptZbjId);
        }
        return yeeMerchantSifMapper.selectList(qw);
    }

    /**
     * 新增结算记录
     *
     * @param yeeMerchantSif 结算记录
     * @return 结果
     */
    public int insert(YeeMerchantSif yeeMerchantSif) {
        yeeMerchantSif.setDelFlag(0);
        yeeMerchantSif.setCreateTime(LocalDateTime.now());
        return yeeMerchantSifMapper.insert(yeeMerchantSif);
    }
    

    /**
     * 添加结算费用   23直播流量费结算   24云服务费结算
     * @param orderAmount 扣款金额
     * @param cmtjyy   结算原因
     * @param jsfl     23直播流量费结算   24云服务费结算
     * @param deptZbjId 直播间id
     * @return
     */
    public YeeMerchantSif startSysSif(BigDecimal orderAmount,String cmtjyy, String jsfl,Long deptZbjId) {
    	YeeMerchantSifAdd yeeMerchantSifAdd = new YeeMerchantSifAdd();
    	yeeMerchantSifAdd.setCmtjyy(cmtjyy);
    	yeeMerchantSifAdd.setJsfl(jsfl);
    	yeeMerchantSifAdd.setOrderAmount(orderAmount);
    	yeeMerchantSifAdd.setToMerchantNo(yeePayConfig.getFeeMerchantNo());
    	return startSif(yeeMerchantSifAdd,deptZbjId);
    }
    
    /**
     * 扣除结算费用   23直播流量费结算   24云服务费结算
     * @param yeeMerchantSifid
     * @param macAddress
     * @return
     */
    public int cwspSysSatrt(long yeeMerchantSifid,String macAddress) {
    	YeeMerchantSifCwsp yeeMerchantSifCwsp = new YeeMerchantSifCwsp();
    	yeeMerchantSifCwsp.setId(yeeMerchantSifid);
    	yeeMerchantSifCwsp.setMacAddress(macAddress);
    	yeeMerchantSifCwsp.setSpjg("2");
    	return cwspSatrt(yeeMerchantSifCwsp);
    }

    /**
     * 出纳提交结算需求
     * @param yeeMerchantSifAdd
     * @return
     */
    public YeeMerchantSif startSif(YeeMerchantSifAdd yeeMerchantSifAdd) {
    	return startSif(yeeMerchantSifAdd, null);
    }
    
    public BigDecimal getFeeAmount(BigDecimal orderAmount,String jsfl) {
    	if("11".equals(jsfl)) {//只有他卡结算收手续费 
         	  return getFeeAmount(orderAmount);
     	}else {
     		return (BigDecimal.ZERO);
     	}
    }
    
    /**
     * 出纳提交结算需求
     * @param yeeMerchantSifAdd
     * @return
     */
    public YeeMerchantSif startSif(YeeMerchantSifAdd yeeMerchantSifAdd,Long deptZbjId) {
      	  YeeMerchant yeeMerchant = yeeMerchantService.getMerchantByNo(yeeMerchantSifAdd.getToMerchantNo());
      	  if(yeeMerchant == null) {
	      		if(yeeMerchantSifAdd.getToMerchantNo().equals(yeePayConfig.getFeeMerchantNo())) {
	      			yeeMerchant = new YeeMerchant();
	      			yeeMerchant.setMerchantNo(yeePayConfig.getFeeMerchantNo());
	      			yeeMerchant.setAccountType(yeePayConfig.getFeeAccountType());//转入方账户类型
	      			yeeMerchant.setMerchantMc("云服务费-直播流量费结算商户");
	      			yeeMerchant.setMerchantBz("云服务费-直播流量费结算商户");
	      			yeeMerchant.setMerchantFl("4");
	      			yeeMerchant.setMerchantLx("1");
	        	}
      	  }
       
      	  

    	  LocalDateTime localDateTime = LocalDateTime.now();
    	  YeeMerchantSif yeeMerchantSif = new YeeMerchantSif(yeeMerchant);
    	  
 		 String jsfl =  yeeMerchantSifAdd.getJsfl();////11 他卡结算 12供应商结算 13提现结算      23直播流量费结算   24云服务费结算
    	  if(jsfl.equals("23") || jsfl.equals("24")) {//23直播流量费结算   24云服务费结算 不登录
              yeeMerchantSif.setDeptZbjId(deptZbjId);
              
    	  }else {
    		  Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
              yeeMerchantSif.setDeptZbjId(qrDeptZbjId);
    	  }
    	  
          
    	  yeeMerchantSif.setCntjTime(localDateTime);
    	  yeeMerchantSif.setCmtjyy(yeeMerchantSifAdd.getCmtjyy());
    	  yeeMerchantSif.setToMerchantNo(yeeMerchantSifAdd.getToMerchantNo());
    	  yeeMerchantSif.setOrderAmount(yeeMerchantSifAdd.getOrderAmount());
    	  yeeMerchantSif.setJsfl(yeeMerchantSifAdd.getJsfl());
    	  //11 他卡结算 12供应商结算 13提现结算      23直播流量费结算   24云服务费结算
    	  String jsfldl = jsfl.substring(0, 1);
    	  if("1".equals(jsfldl)) {//第一位为1的要收手续费
    		  yeeMerchantSif.setCnryid(SecurityUtils.getUserId());
        	  yeeMerchantSif.setCreateBy(yeeMerchantSif.getCnryid());
        	  yeeMerchantSif.setCnryxm(SecurityUtils.getNickName());
        	  if("11".equals(jsfl)) {//只有他卡结算收手续费 
            	  yeeMerchantSif.setFeeAmount(getFeeAmount(yeeMerchantSifAdd.getOrderAmount()));
        	  }else {
            	  yeeMerchantSif.setFeeAmount(BigDecimal.ZERO);
        	  }
    	  }else {
    		  yeeMerchantSif.setCnryid(-10L);
        	  yeeMerchantSif.setCreateBy(-10L);
        	  yeeMerchantSif.setCnryxm("系统扣费出纳");
    		  //不收手续费
        	  yeeMerchantSif.setFeeAmount(BigDecimal.ZERO);
    	  }
    	  
    	  
    	  yeeMerchantSif.setJszt("1");//结算状态1财务待审批2财务审批通过3财务审批不通过
    	  yeeMerchantSif.setFeeZt("0");//0未处理
    	  yeeMerchantSif.setB2bZt("0");//0未处理
    	  yeeMerchantSif.setB2cZt("0");//0未处理
    	  yeeMerchantSif.setDelFlag(0);
          yeeMerchantSif.setCreateTime(localDateTime);
           yeeMerchantSifMapper.insert(yeeMerchantSif);
           return yeeMerchantSif;
	}
     
    /**
     * 财务审批结算
     * @param yeeMerchantSifAdd
     * @return
     */
    public int cwspSatrt(YeeMerchantSifCwsp yeeMerchantSifCwsp) {
    	String spjg = yeeMerchantSifCwsp.getSpjg();
    	YeeMerchantSif yeeMerchantSif =  this.selectById(yeeMerchantSifCwsp.getId());
    	if(yeeMerchantSif==null) {
            throw new RuntimeException("操作失败！，没有查询到结算记录");
    	}
    	if(!yeeMerchantSif.getJszt().equals("1")) {
            throw new RuntimeException("当前结算状态不是带处理状态，请刷新页面重试！");
    	}
     	  //11 他卡结算 12供应商结算 13提现结算      23直播流量费结算   24云服务费结算
  	    String jsfl = yeeMerchantSif.getJsfl();
  	    String jsfldl = jsfl.substring(0, 1);
    	int num = 0;
    	if(spjg.equals("2") || spjg.equals("3")) {
    		  LocalDateTime localDateTime = LocalDateTime.now();
    		  
    	  	  if("1".equals(jsfldl)) {//第一位为1的要收手续费
    	  		  yeeMerchantSif.setCwryid(SecurityUtils.getUserId());
        	  	  yeeMerchantSif.setCwryxm(SecurityUtils.getNickName());
	       	  }else {
	       		  yeeMerchantSif.setCnryid(-11L);
	        	  yeeMerchantSif.setCnryxm("系统扣费财务");
	       	  }
    	  	 
    	  	  yeeMerchantSif.setCwtjTime(localDateTime);
    	  	  yeeMerchantSif.setUpdateTime(localDateTime);
    	  	  yeeMerchantSif.setJszt(spjg);//结算状态1财务待审批2财务审批通过3财务审批不通过
    	  	  yeeMerchantSif.setCwbtgyy(yeeMerchantSifCwsp.getCwbtgyy());
    	  	  yeeMerchantSif.setB2cBz(yeeMerchantSifCwsp.getB2cBz());
    	  	  yeeMerchantSif.setUpdateBy(yeeMerchantSif.getCwryid());
    	  	  yeeMerchantSif.setMacAddress(yeeMerchantSifCwsp.getMacAddress());
    	  	  num =  update(yeeMerchantSif);
    		  if(num!=1) {
    	            throw new RuntimeException("操作失败！，结算记录不存在或删除");
    		  }
    	}else {
            throw new RuntimeException("审批结果状态异常！没有["+spjg+"]这个状态");
    	}
    	if(spjg.equals("3")) {
    		return num;//过3财务审批不通过直接返回
    	}
    	return d2bAddB2cSatrt(yeeMerchantSif);
	}
    
    /**
     * 转账并且提现
     * @param yeeMerchantSif
     * @return
     */
    public int d2bAddB2cSatrt(YeeMerchantSif yeeMerchantSif) {
    	if("2".equals(yeeMerchantSif.getB2bZt())) {//本来就是成功的
			return 1;// 不发起
		}
    	if(yeeMerchantSif.getFromMerchantNo()==null) {
    		String  merchantNo = yeePayService.getBaseMerchantNo(null);
    		if(merchantNo==null) {
    			 throw new RuntimeException("操作失败！付款商户未配置");
    		}
    		yeeMerchantSif.setFromMerchantNo(merchantNo);
    	}
  	  
    	if(!yeeMerchantSif.getFromMerchantNo().equals(yeeMerchantSif.getToMerchantNo())) {//同一个账号不走b2c
			//发起结算
			TransferB2bOrderMgTransferOrderRespDTOResult d2bResult = yeePayService.transferB2bOrder(yeeMerchantSif);
	        if(!"UA00000".equals(d2bResult.getReturnCode())) {
	        	yeeMerchantSif.setB2bTime(LocalDateTime.now());
	        	yeeMerchantSif.setB2bYy("code:"+d2bResult.getReturnCode()+" msg: "+d2bResult.getReturnMsg());
	        	yeeMerchantSif.setB2bZt("3");//到账状态1处理中2成功3失败
	        	yeeMerchantSif.setB2bRequestNo(d2bResult.getOrderNo());
	        	return update(yeeMerchantSif);//失败直接返回
	        }else {
	        	yeeMerchantSif.setB2bTime(LocalDateTime.now());
	        	yeeMerchantSif.setB2bZt("2");//到账状态1处理中2成功3失败 //回调修改状态
	        	yeeMerchantSif.setB2bRequestNo(d2bResult.getOrderNo());
	        }
    	}else {
    		yeeMerchantSif.setB2bTime(LocalDateTime.now());
        	yeeMerchantSif.setB2bZt("2");//到账状态1处理中2成功3失败 //回调修改状态
        	yeeMerchantSif.setB2bRequestNo(null);
    	}
    	return d2cSatrt(yeeMerchantSif);
    }
    
    /**
     * 提现
     * @param yeeMerchantSif
     * @return
     */
    public int d2cSatrt(YeeMerchantSif yeeMerchantSif) {
		feeStart(yeeMerchantSif);
    	if("2".equals(yeeMerchantSif.getB2cZt())) {//本来就是成功的
			return 1;// 不发起
		}
    	  String jsfl = yeeMerchantSif.getJsfl();
		  String jsfldl = jsfl.substring(0, 1);
		  //11 他卡结算 12供应商结算 13提现结算      23直播流量费结算   24云服务费结算
		  if("2".equals(jsfldl)) {//第一位为2的不走B2c
	    		yeeMerchantSif.setB2cTime(LocalDateTime.now());
	        	yeeMerchantSif.setB2cZt("2");//到账状态1处理中2成功3失败 //回调修改状态
	        	yeeMerchantSif.setFeeTime(LocalDateTime.now());
	        	yeeMerchantSif.setFeeZt("2");//到账状态1处理中2成功3失败 //回调修改状态
		  }else {
			   WithdrawOrderWithdrawOrderRespDTOResult d2cResult =  yeePayService.transferB2cOrder(yeeMerchantSif);
		       if(!"UA00000".equals(d2cResult.getReturnCode())) {
		        	yeeMerchantSif.setB2cTime(LocalDateTime.now());
		        	yeeMerchantSif.setB2cYy("code:"+d2cResult.getReturnCode()+" msg: "+d2cResult.getReturnMsg());
		        	yeeMerchantSif.setB2cZt("3");//到账状态1处理中2成功3失败
		        	yeeMerchantSif.setB2cRequestNo(d2cResult.getOrderNo());
		        	return update(yeeMerchantSif);//失败直接返回
		       }else {
		        	yeeMerchantSif.setB2cRequestNo(d2cResult.getOrderNo());
		    		yeeMerchantSif.setB2cTime(LocalDateTime.now());
		        	yeeMerchantSif.setB2cZt("1");//到账状态1处理中2成功3失败 //回调修改状态
		       }
		  }
       
    	return update(yeeMerchantSif);
    }
    /**
     * b2c回调
     * @param ymsfid
     * @param success
     * @param b2cRequestNo
     * @param sbyy
     * @return
     */
    public int d2cNotify(Long ymsfid,boolean success, String b2cRequestNo,String sbyy) {
    	YeeMerchantSif yeeMerchantSif =  this.selectById(ymsfid);
		if(!"2".equals(yeeMerchantSif.getB2cZt())) {//不等于成功才发起
			yeeMerchantSif.setB2cTime(LocalDateTime.now());
	    	yeeMerchantSif.setB2cRequestNo(b2cRequestNo);
	    	if(success) {
	        	yeeMerchantSif.setB2cZt("2");//到账状态1处理中2成功3失败
	    	}else {
	        	yeeMerchantSif.setB2cZt("3");//到账状态1处理中2成功3失败
	        	yeeMerchantSif.setB2cYy(sbyy);
	    	}
		}
    	
    	boolean sfFee = true;
    	if (yeeMerchantSif.getFeeAmount().compareTo(BigDecimal.ZERO)==0){//未0不收手续费
    		sfFee = false;
    		yeeMerchantSif.setFeeTime(LocalDateTime.now());
        	yeeMerchantSif.setFeeZt("2");//到账状态1处理中2成功3失败 //回调修改状态
    	}
    			
    	int num =  update(yeeMerchantSif);
    	if(success && sfFee) {
    		feeStart(yeeMerchantSif);
    	}
    	return num;
    }
    
    /**
     * 扣手续费
     * @param yeeMerchantSif
     * @return
     */
    public int feeStart(YeeMerchantSif yeeMerchantSif) {
    	try {
    		if(!"2".equals(yeeMerchantSif.getB2bZt())) {//b2b 没有成功不扣手续费
    			return 1;// 不发起
    		}
    		
    		if("2".equals(yeeMerchantSif.getFeeZt())) {//本来就是成功的
    			return 1;// 不发起
    		}
    		
        	if (yeeMerchantSif.getFeeAmount().compareTo(BigDecimal.ZERO)==0){//未0不收手续费
        		yeeMerchantSif.setFeeTime(LocalDateTime.now());
            	yeeMerchantSif.setFeeZt("2");//到账状态1处理中2成功3失败 //回调修改状态
            	update(yeeMerchantSif);
        		return 1;// 手续费为0 
        	}
    		TransferB2bOrderMgTransferOrderRespDTOResult d2bResult  = yeePayService.transferB2bByFee(yeeMerchantSif);
        	//发起结算
            if(!"UA00000".equals(d2bResult.getReturnCode())) {
            	yeeMerchantSif.setFeeTime(LocalDateTime.now());
            	yeeMerchantSif.setFeeYy("code:"+d2bResult.getReturnCode()+" msg: "+d2bResult.getReturnMsg());
            	yeeMerchantSif.setFeeZt("3");//到账状态1处理中2成功3失败
            	yeeMerchantSif.setFeeRequestNo(d2bResult.getOrderNo());
            	return update(yeeMerchantSif);//失败直接返回
            }else {
            	yeeMerchantSif.setFeeTime(LocalDateTime.now());
            	yeeMerchantSif.setFeeZt("2");//到账状态1处理中2成功3失败 //回调修改状态
            	yeeMerchantSif.setFeeRequestNo(d2bResult.getOrderNo());
            }
        	return update(yeeMerchantSif);
		} catch (Exception e) {
			yeeMerchantSif.setFeeTime(LocalDateTime.now());
        	yeeMerchantSif.setFeeYy(e.getMessage());
        	yeeMerchantSif.setFeeZt("3");//到账状态1处理中2成功3失败
        	return update(yeeMerchantSif);//失败直接返回
		}
    	
    }
 
    
    /**
     * 修改结算记录
     *
     * @param yeeMerchantSif 结算记录
     * @return 结果
     */
    public int update(YeeMerchantSif yeeMerchantSif) {
        return yeeMerchantSifMapper.updateById(yeeMerchantSif);
    }

    /**
     * 批量删除结算记录
     *
     * @param ids 需要删除的结算记录主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        return yeeMerchantSifMapper.updateDelFlagByIds(ids);
    }

    /**
     * 删除结算记录信息
     *
     * @param id 结算记录主键
     * @return 结果
     */
    public int deleteById(Long id) {
        Long[] ids = {id};
        return yeeMerchantSifMapper.updateDelFlagByIds(ids);
    }
     
    
    
    
  

}
